﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.zoo.ui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Mechanics.GameMechanicInterfaces.*;
    import org.aswing.*;
    import Classes.*;
    import flash.filters.*;
    import Display.*;
    import flash.text.*;
    import org.aswing.event.*;
    import Modules.zoo.events.*;
    import Display.aswingui.*;

    public class ZooCell extends DataItemCell {

        protected var m_itemIconPane:JPanel;
        protected var m_cardPane:JPanel;
        private var m_iconPane:AssetPane;
        private var m_counter:JTextField;
        private var m_count:int;
        private var m_bHasActiveAnimals:Boolean = true;
        protected var m_counterPane:JPanel;
        private var m_placeBtn:CustomButton;
        private var m_buyBtn:CustomButton;

        public function ZooCell(_arg1:LayoutManager=null){
            var _local2:LayoutManager = _arg1;
            if (_local2 == null){
                _local2 = new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 2, SoftBoxLayout.CENTER);
            };
            super(_local2);
        }
        override public function setCellValue(_arg1):void{
            _arg1 = (_arg1 as Object);
            m_item = _arg1;
            var _local2:String = Global.gameSettings.getImageByName(m_item.name, m_item.iconImageName);
            m_loader = LoadingManager.load(_local2, onIconLoad, LoadingManager.PRIORITY_HIGH);
            ZooDialogView.currentInstance.addTooltipTarget(this, ZLoc.t("Dialogs", ("ZooDialog_animalTooltip_" + m_item.name)));
            this.buildCell();
        }
        public function get animal():Item{
            return (this.m_item);
        }
        public function get animalName():String{
            return (this.m_item.name);
        }
        public function get count():int{
            return (this.m_count);
        }
        public function changeChrome():void{
            var _local1:DisplayObject;
            var _local2:DisplayObject;
            if (!this.m_bHasActiveAnimals){
                this.m_bHasActiveAnimals = true;
                _local1 = (new (ZooDialog.assetDict["card_available_selected"])() as DisplayObject);
                _local2 = (new (ZooDialog.assetDict["counter_active"])() as DisplayObject);
                m_itemIcon.filters = [];
                ASwingHelper.setSizedBackground(this.m_counterPane, _local2);
                ASwingHelper.setSizedBackground(this.m_cardPane, _local1);
                this.m_placeBtn.setEnabled(this.m_bHasActiveAnimals);
            };
        }
        public function changeCounter(_arg1:int):void{
            var _local2:ZooEnclosure;
            this.m_count = _arg1;
            this.m_counter.setText(((this.m_count + "/") + m_item.inventoryLimit));
            if (ZooDialogView.currentInstance.currZooEnclosure){
                _local2 = ZooDialogView.currentInstance.currZooEnclosure;
                this.m_buyBtn.setEnabled((_local2.getNumSpecificAnimal(m_item.name) < m_item.inventoryLimit));
            };
            this.m_placeBtn.setEnabled((this.m_count > 0));
        }
        override protected function initializeCell():void{
            this.m_itemIconPane.removeAll();
            var _local1:Number = 0.212671;
            var _local2:Number = 0.71516;
            var _local3:Number = 0.072169;
            var _local4:Array = [_local1, _local2, _local3, 0, 0, _local1, _local2, _local3, 0, 0, _local1, _local2, _local3, 0, 0, 0, 0, 0, 1, 0];
            if (this.m_bHasActiveAnimals){
                m_itemIcon.filters = [];
            } else {
                m_itemIcon.filters = [new ColorMatrixFilter(_local4)];
            };
            this.m_iconPane = new AssetPane(m_itemIcon);
            this.m_itemIconPane.addEventListener(MouseEvent.CLICK, this.placeItem, false, 0, true);
            ASwingHelper.setSizedBackground(this.m_itemIconPane, m_itemIcon);
            ASwingHelper.prepare(this);
            ASwingHelper.prepare(this.m_itemIconPane);
            var _local5:Number = ((this.m_cardPane.getWidth() - m_itemIcon.width) / 2);
            var _local6:Number = ((this.m_cardPane.getHeight() - m_itemIcon.height) / 2);
            ASwingHelper.setEasyBorder(this.m_itemIconPane, _local6, _local5);
        }
        protected function buildCell():void{
            var _local6:DisplayObject;
            var _local9:String;
            this.m_count = 0;
            if (ZooDialogView.currentInstance.currStorageMechanic){
                this.m_count = ZooDialogView.currentInstance.currStorageMechanic.getCount(m_item.name);
            };
            var _local1:ISlotMechanic = ZooDialogView.currentInstance.currSlotMechanic;
            var _local2:int;
            if (_local1){
                _local2 = _local1.numSlots;
            };
            var _local3:int;
            var _local4:int;
            while (_local4 < _local2) {
                if (!_local1.isSlotFilled(_local4)){
                } else {
                    _local9 = _local1.getSlot(_local4);
                    if (_local9 == this.animalName){
                        _local3++;
                    };
                };
                _local4++;
            };
            if ((((this.m_count == 0)) && ((_local3 == 0)))){
                this.m_bHasActiveAnimals = false;
            } else {
                this.m_bHasActiveAnimals = true;
            };
            var _local5:DisplayObject = (new (ZooDialog.assetDict["card_available_selected"])() as DisplayObject);
            if (this.m_bHasActiveAnimals){
                _local5 = (new (ZooDialog.assetDict["card_available_selected"])() as DisplayObject);
            } else {
                _local5 = (new (ZooDialog.assetDict["card_available_unselected"])() as DisplayObject);
            };
            this.m_cardPane = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_itemIconPane = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_counter = ASwingHelper.makeTextField((((this.m_count + "/") + m_item.inventoryLimit) + " "), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 10, EmbeddedArt.whiteTextColor);
            this.m_counter.getTextField().gridFitType = GridFitType.SUBPIXEL;
            this.m_counter.setPreferredWidth(38);
            this.m_counter.setPreferredHeight(15);
            this.m_counterPane = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            if (this.m_bHasActiveAnimals){
                _local6 = (new (ZooDialog.assetDict["counter_active"])() as DisplayObject);
            } else {
                _local6 = (new (ZooDialog.assetDict["counter_inactive"])() as DisplayObject);
            };
            ASwingHelper.setSizedBackground(this.m_counterPane, _local6);
            this.m_counterPane.append(this.m_counter);
            ASwingHelper.prepare(this.m_counterPane);
            ASwingHelper.setSizedBackground(this.m_cardPane, _local5);
            var _local7:AssetIcon = new AssetIcon(new (ZooDialog.assetDict["icon_cash"])());
            this.m_buyBtn = new CustomButton(String(m_item.cash), _local7, "CashButtonUI");
            this.m_buyBtn.addActionListener(this.buyItem, 0, true);
            this.m_placeBtn = new CustomButton(ZLoc.t("Dialogs", "Place"), null, "GreenButtonUI");
            this.m_placeBtn.setEnabled(this.m_bHasActiveAnimals);
            var _local8:int = ((((Global.localizer.langCode == "de")) || ((Global.localizer.langCode == "id")))) ? 10 : 12;
            this.m_placeBtn.setFont(new ASFont(EmbeddedArt.TITLE_FONT, _local8, false, false, false, EmbeddedArt.advancedFontProps));
            this.m_placeBtn.setMargin(new Insets(1, 0, 1, 0));
            this.m_placeBtn.setPreferredWidth(_local5.width);
            this.m_placeBtn.addActionListener(this.placeItem, 0, true);
            if (!_local1){
                this.m_buyBtn.setEnabled(false);
                this.m_placeBtn.setEnabled(false);
            };
            this.m_cardPane.append(this.m_itemIconPane);
            this.m_cardPane.addChild(this.m_counterPane);
            this.m_counterPane.x = (_local5.width - this.m_counterPane.getWidth());
            this.m_counterPane.y = 74;
            this.appendAll(ASwingHelper.verticalStrut(5), this.m_cardPane, this.m_placeBtn);
            if (!m_item.requestOnly){
                this.append(this.m_buyBtn);
            };
            this.changeCounter(this.m_count);
        }
        protected function buyItem(_arg1:AWEvent):void{
            if (Global.player.cash >= m_item.cash){
                dispatchEvent(new ZooDialogEvent(ZooDialogEvent.BUY_NEW_ANIMAL, m_item.name, true));
            } else {
                UI.displayImpulseBuyPopup(ImpulseBuy.TYPE_MARKET_CASH);
            };
        }
        protected function placeItem(_arg1:Event):void{
            if (this.m_count > 0){
                m_itemIcon = null;
                m_itemIcon = (new EmbeddedArt.hud_act_construction() as DisplayObject);
                ASwingHelper.prepare(this.parent.parent.parent);
                dispatchEvent(new ZooDialogEvent(ZooDialogEvent.ADD_ANIMAL_TO_DISPLAY, m_item.name, true));
            };
        }

    }
}//package Modules.zoo.ui 
